package com.nj.dms.controller;

import java.io.IOException;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.nj.dms.bean.FileBean;
import com.nj.dms.dao.FileDao;
import com.nj.dms.dao.impl.FileDaoImpl;

/**
 * 保存修改的文件信息
 */
@WebServlet("/SaveFile")
public class SaveFile extends HttpServlet {
	private static final long serialVersionUID = 1L;

	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		// 获取表单的数据
		int id = new Integer(request.getParameter("id"));

		String version = request.getParameter("version");
		int thick = new Integer(request.getParameter("thick"));
		String description = request.getParameter("description");

		int audit = thick == 1 ? 1 : 0;

		FileBean fileBean = new FileBean(id, description, thick, audit, version, new Date());

		FileDao fileDao = new FileDaoImpl();

		if (fileDao.saveFile(fileBean)) {
			// 文件信息保存成功，跳转到文件列表

			response.sendRedirect("MyFileList");

		} else {

			// 文件信息保存失败，跳转到文件修改页

			response.sendRedirect("UpdateFile?id=" + id);

		}

	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doGet(request, response);
	}

}
